﻿Imports MapWinGIS
Imports System.ComponentModel

<Serializable()> Public Class ESPolygonLayer
    Inherits EditableShapeLayer


    Public Sub New(ByVal ln As String, ByVal tl As BaseTool)
        MyBase.New(ln, tl)

        linetype = FtkLineStipple.lsNone
        lineColor = Drawing.Color.Black
        lineWidth = 1

        ' fillColor = Drawing.Color.White
        filltype = FtkFillStipple.fsNone
        bFill = True

        fillLineclr = Drawing.Color.Black


    End Sub

    '这些都是为了讨厌的VB事件逻辑啊
    Protected Sub New( _
      ByVal info As System.Runtime.Serialization.SerializationInfo, _
      ByVal context As System.Runtime.Serialization.StreamingContext)

        MyBase.New(info, context)

    End Sub


    '填充相关
#Region "填充相关"

    Protected fillColor As Drawing.Color
    <CategoryAttribute("外观设置"), _
 DisplayName("填充颜色")> _
  Public Property FillColorProperty() As Drawing.Color
        Get
            Return fillColor
        End Get
        Set(ByVal value As Drawing.Color)
            fillColor = value
            OnFlColorChanged()
        End Set
    End Property
    Protected Sub OnFlColorChanged()

        Dim ucl As UInt32 = RGB(fillColor.R, fillColor.G, fillColor.B)
        mapwin.set_ShapeLayerFillColor(m_iLayer, ucl)

    End Sub

    Protected fillLineclr As Drawing.Color
    <CategoryAttribute("外观设置"), _
 DisplayName("填充线条颜色")> _
    Public Property FillLineTypeProperty() As Drawing.Color
        Get
            Return fillLineclr
        End Get
        Set(ByVal value As Drawing.Color)
            If fillLineclr = value Then
            Else
                fillLineclr = value
                Me.OnFillLineColorChanged()
            End If
        End Set
    End Property

    Protected Sub OnFillLineColorChanged()

        Dim ucl As UInt32 = RGB(fillLineclr.R, fillLineclr.G, fillLineclr.B)

        mapwin.set_ShapeLayerStippleColor(m_iLayer, ucl)





    End Sub

    Protected filltype As FtkFillStipple
    <CategoryAttribute("外观设置"), _
 DisplayName("填充类型")> _
  Public Property FilltypeProperty() As FtkFillStipple
        Get
            Return filltype
        End Get
        Set(ByVal value As FtkFillStipple)
            filltype = value
            onFlTypeChanged()
        End Set
    End Property
    Protected Sub onFlTypeChanged()

        Dim temp As MapWinGIS.tkFillStipple = Me.getFillType(filltype)
        mapwin.set_ShapeLayerFillStipple(m_iLayer, temp)

    End Sub

    Dim bFill As Boolean
    <CategoryAttribute("外观设置"), _
 DisplayName("是否填充")> _
  Public Property BFillProperty() As Boolean
        Get
            Return bFill
        End Get
        Set(ByVal value As Boolean)
            bFill = value
            OnBFlChanged()
        End Set
    End Property

    Protected Sub OnBFlChanged()
        mapwin.set_ShapeLayerDrawFill(m_iLayer, bFill)
    End Sub





#End Region


#Region "线条相关"



    '线条相关以及对应的处理程序

    Dim linetype As FtkLineStipple

    <CategoryAttribute("外观设置"), _
   DisplayName("线条类型")> _
   Public Overridable Property LinetypeProperty() As FtkLineStipple
        Get
            Return linetype
        End Get
        Set(ByVal value As FtkLineStipple)
            linetype = value

            Me.OnLnTypeChanged(linetype)

        End Set
    End Property


    Dim lineColor As Drawing.Color
    <CategoryAttribute("外观设置"), _
    DisplayName("线条颜色")> _
    Public Overridable Property LineColorProperty() As Drawing.Color
        Get
            Return lineColor
        End Get
        Set(ByVal value As Drawing.Color)
            lineColor = value
            Me.OnLnClChanged(lineColor)
        End Set
    End Property
    Dim lineWidth As Double

    <CategoryAttribute("外观设置"), _
   DisplayName("线条宽度")> _
   Public Overridable Property LineWidthProperty() As Double
        Get
            Return lineWidth
        End Get
        Set(ByVal value As Double)
            lineWidth = value

            Me.OnLnWdChanged(lineWidth)

        End Set
    End Property

    Private Sub OnLnWdChanged(ByVal d As Double)

        mapwin.set_ShapeLayerLineWidth(m_iLayer, d)
    End Sub
    Private Sub OnLnClChanged(ByVal cl As Drawing.Color)

        Dim ucl As UInt32 = RGB(cl.R, cl.G, cl.B)
        mapwin.set_ShapeLayerLineColor(m_iLayer, ucl)
    End Sub
    Private Sub OnLnTypeChanged(ByVal lt As MapWinGIS.tkLineStipple)

        Dim temp As MapWinGIS.tkLineStipple = Me.getLineType(lt)
        mapwin.set_ShapeLayerLineStipple(m_iLayer, temp)



    End Sub

#End Region

    Public Overrides Sub AddEntityRange(ByVal lst As System.Collections.Generic.List(Of FEntity))

        For Each en As FEntity In lst

            If en.ptlist.Length < 20 Then

                dicKeyPts.Add(en.sn, en)

            End If



        Next





        Dim ptlist As MapWinGIS.Point()
        Dim sn As String
        Dim desc As String

        Dim sfPoly As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)



        sfPoly.StartEditingShapes(True)



        For Each en As FEntity In lst



            sn = en.sn
            ptlist = en.ptlist
            desc = en.desc


            If ptlist.Length < 1 Then

            Else




                '增加封边点，使成为封闭的多边形

                Dim nl As Integer = ptlist.Length


                ReDim Preserve ptlist(nl)

                ptlist(nl) = ptlist(0)













                Dim shp As New Shape
                shp.ShapeType = ShpfileType.SHP_POLYGON
                Dim ptIndex As Integer = 0

                Dim n As Integer = ptlist.Length



                For i As Integer = 1 To n

                    shp.InsertPoint(ptlist(i - 1), ptIndex)


                Next




                Dim shpIndex As Integer = sfPoly.NumShapes

                sfPoly.EditInsertShape(shp, shpIndex)

                sfPoly.EditCellValue(0, shpIndex, sn)
                sfPoly.EditCellValue(1, shpIndex, desc)




            End If





        Next

        sfPoly.StopEditingShapes()


        '由于新增加了对象，所以需要根据现有状态，对对象操作

        Me.DrawText()

        mapwin.Redraw()

    End Sub

    Public Overrides Function AddEntity(ByVal sn As String, ByVal ptlist() As MapWinGIS.Point, ByVal desc As String) As Integer

        If ptlist.Length < 1 Then
            mapwin.Redraw()
            Return -1
        End If


        '关键节点，呵呵,因为圆形的存在，还是先不用存了，影响效率
        If ptlist.Length < 20 Then

            Dim en As New FEntity(sn, ptlist, desc)
            dicKeyPts.Add(sn, en)

        End If






        '增加封边点，使成为封闭的多边形





        Dim nl As Integer = ptlist.Length


        ReDim Preserve ptlist(nl)

        ptlist(nl) = ptlist(0)




        Dim sfPoly As Shapefile = mapwin.get_GetObject(m_iLayer)



        sfPoly.StartEditingShapes(True)





        Dim shp As New Shape
        shp.ShapeType = ShpfileType.SHP_POLYGON
        Dim ptIndex As Integer = 0

        Dim n As Integer = ptlist.Length



        For i As Integer = 1 To n

            shp.InsertPoint(ptlist(i - 1), ptIndex)


        Next




        Dim shpIndex As Integer = sfPoly.NumShapes

        sfPoly.EditInsertShape(shp, shpIndex)

        sfPoly.EditCellValue(0, shpIndex, sn)
        sfPoly.EditCellValue(1, shpIndex, desc)

        sfPoly.StopEditingShapes()





        Me.DrawText()


        mapwin.Redraw()

        Return shpIndex

    End Function

    Public Overrides Sub InitNewLayer(ByRef m As AxMapWinGIS.AxMap, ByVal OutPath As String)


        Dim sf As New Shapefile
        Dim strPath(2) As String


        'strPath(0) = "C:\temp\" + lname + ".shp"
        'strPath(1) = "C:\temp\" + lname + ".dbf"
        'strPath(2) = "C:\temp\" + lname + ".shx"

        'If System.IO.File.Exists(strPath(0)) Then
        '    System.IO.File.Delete(strPath(0))
        '    System.IO.File.Delete(strPath(1))
        '    System.IO.File.Delete(strPath(2))

        'End If

        sf.CreateNew("", MapWinGIS.ShpfileType.SHP_POLYGON)




        Dim f As New Field

        f = New Field
        f.Name = "Name"
        f.Type = FieldType.STRING_FIELD
        f.Width = 50

        Dim fd As New Field

        fd = New Field
        fd.Name = "desc"
        fd.Type = FieldType.STRING_FIELD
        fd.Width = 50

        sf.StartEditingShapes(True)
        sf.EditInsertField(f, 0)
        sf.EditInsertField(fd, 1)

        sf.StopEditingShapes(True, True)


        '加入载入的逻辑

        Dim strSave As String = OutPath + lname + ".shp"

        strPath(0) = OutPath + lname + ".shp"
        strPath(1) = OutPath + lname + ".dbf"
        strPath(2) = OutPath + lname + ".shx"

        For i As Integer = 0 To strPath.Length - 1
            If System.IO.File.Exists(strPath(i)) Then
                Try
                    System.IO.File.SetAttributes(strPath(i), IO.FileAttributes.Normal)
                    System.IO.File.Delete(strPath(i))
                Catch ex As Exception

                End Try

            End If
        Next

        Dim bS As Boolean = sf.SaveAs(strSave)

        If Not bS Then
            System.Windows.Forms.MessageBox.Show(lname + "文件创建失败！，请查看电脑软件使用权限")

        End If

        fname = strSave

        Me.InitLayer(m)

    End Sub


    Public Overrides Sub InitProp()

        Me.OnLnClChanged(lineColor)
        Me.OnLnWdChanged(lineWidth)
        Me.OnLnTypeChanged(linetype)


        Me.OnFillLineColorChanged()


        OnFlColorChanged()
        onFlTypeChanged()
        OnBFlChanged()


    End Sub

 

    ''' <summary>
    ''' 只在加载外部shp文件时，在最开始部分调用此函数
    ''' 非常规的调用会引发程序错误
    ''' </summary>
    ''' <remarks></remarks>
    Public Overrides Sub BuildKeyPtsFromFile()
        Dim sf As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)
        For i As Integer = 0 To sf.NumShapes
            Dim en As New FEntity()
            en.sn = Me.GetShapeFieldString(i, 0)
            en.desc = Me.GetShapeFieldString(i, 1)
            en.ptlist = sf.QuickPoints(i, sf.Shape(i).numPoints)


        Next

    End Sub

    '额外添加的功能
    ''' <summary>
    ''' '合并所有的图层形状
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub IntegratedAllShapes()


        Dim shpfile As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)


        '刘改

        Dim lstShapes As New List(Of MapWinGIS.Shape)  '要储存的结果Shape

        '合并Shape
        UnionShape.UnionShape(shpfile, lstShapes)
    


        '删除现有图层的文件

        'Dim convexShp As MapWinGIS.Shape


        Dim shpIndex As Integer

        shpfile.StartEditingShapes()

        shpfile.EditClear()





        For Each shp As MapWinGIS.Shape In lstShapes

            '这个管用吗？
            'convexShp = shp.ConvexHull()

            'If convexShp Is Nothing Then
            '    shpfile.EditInsertShape(shp, shpIndex)

            'Else

            '    shpfile.EditInsertShape(convexShp, shpIndex)

            'End If

            shpfile.EditInsertShape(shp, shpIndex)


            shpfile.EditCellValue(0, shpIndex, "shapeUnion" + shpIndex.ToString())
            shpfile.EditCellValue(1, shpIndex, "并集轮廓" + shpIndex.ToString())

        Next







        shpfile.StopEditingShapes()






        'Me.RemoveAllEntity()

        'Me.AddEntity("shapeUnion", , "并集轮廓")



    End Sub



    '''' <summary>
    '''' 合并Shape
    '''' </summary>
    '''' <param name="Shp">要合并的shape文件</param>
    '''' <param name="ResultShape">合并后的shape集合</param>
    '''' <remarks></remarks>
    'Private Sub UnionShape(ByVal Shp As MapWinGIS.Shapefile, ByRef ResultShape As List(Of Shape))

    '    Dim TempShapeList As New List(Of Shape)
    '    For i As Integer = 0 To Shp.NumShapes - 1
    '        TempShapeList.Add(Shp.Shape(i))
    '    Next

    '    Dim u As New MapWinGIS.Utils
    '    Dim nList As New List(Of Integer)


    '    Dim BigShape As New Shape
    '    BigShape = TempShapeList.Item(0)

    '    While TempShapeList.Count > 1

    '        TempShapeList.RemoveAt(0)
    '        Dim B As Boolean = False

    '        Dim OldShape As New Shape
    '        OldShape = BigShape

    '        Dim j As Integer
    '        For j = 0 To TempShapeList.Count - 1

    '            Dim TempShape As New Shape
    '            TempShape = TempShapeList.Item(j)

    '            BigShape = u.ClipPolygon(PolygonOperation.UNION_OPERATION, BigShape, TempShape)

    '            If BigShape IsNot Nothing Then
    '                B = True
    '                Exit For
    '            Else
    '                BigShape = OldShape
    '            End If
    '        Next

    '        If B = True Then
    '            TempShapeList.RemoveAt(j)
    '            TempShapeList.Add(BigShape)
    '        Else
    '            ResultShape.Add(BigShape)

    '        End If

    '        If TempShapeList.Count = 1 Then
    '            ResultShape.Add(TempShapeList.Item(0))
    '        End If

    '        BigShape = TempShapeList.Item(0)

    '    End While


    'End Sub


End Class
